package main

import (
	"fmt"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"time"
)

func main() {
	e := echo.New()
	e.Use(middleware.Logger())
	//使用一：
	//e.Use(middleware.RequestID())

	//使用二：自定义配置
	e.Use(middleware.RequestIDWithConfig(CustomRequestIDConfig()))

	e.GET("/index", Index)
	e.Start(":80")
}
func Index(c echo.Context) error {
	requestId := c.Response().Header().Get(echo.HeaderXRequestID)
	return c.String(200, requestId)
}

func CustomRequestIDConfig() middleware.RequestIDConfig {
	return middleware.RequestIDConfig{
		Skipper: middleware.DefaultSkipper,
		Generator: func() string {
			nano := time.Now().UnixNano()
			return fmt.Sprintf("%d", nano)
		},
	}
}
